if (!window.Vue) {
    throw '请引入Vue'
}

// 注册组件
Vue.component('ui-form', {
    template: '#form',
    props: { title: String, options: Object, model: Object, value: Object },
    data: function() {
        return {
            form: {
                select: '',
                radio: '',
                tags: [],
                textarea: ''
            }
        }
    },
    watch: {
        form: {
            immediate: true,
            deep: true,
            handler(value) {
                this.$emit('input', value)
            }
        }
    },
    computed: {
        isMultiple() {
            return this.options.tag ? this.options.tag.multiple : false
        }
    },
    methods: {
        handleToggleTag: function(tag) {
            var index = this.form.tags.findIndex(function(value) {
                return value === tag.value
            })
            if (index === -1) {
                if (this.isMultiple) {
                    this.form.tags.push(tag.value)
                } else {
                    this.form.tags = [tag.value]
                }
            } else {
                this.form.tags.splice(index, 1)
            }
        }
    },
    created() {
        this.form = Object.assign({}, this.form, this.value)
    }
})

// 表单配置
const formList = [
    {
        title: '服务评价',
        options: {
            radio: {
                model: 'radio',
                values: fweav
            },
            tag: {
                multiple: true,
                model: 'tag',
                values: fwtag
            },
            textarea: {
                model: 'textarea'
            }
        }
    },
    {
        title: '菜品评价',
        options: {
            radio: {
                model: 'radio',
                values: cpeva
            },
            tag: {
                model: 'tag',
                values: cptag
            },
            textarea: {
                model: 'textarea'
            }
        }
    }
]

const vm = new Vue({
    data: {
        formList: formList,
        model: {
            num: '',
            code: '',
            forms: [
                { radio: '46', tags: [], textarea: '' },
                { radio: '56', tags: [], textarea: '' }
            ]
        }
    },
    methods: {
        handleSubmit() {
            console.log(this.model)
            console.log(JSON.stringify(this.model))
            if (this.model.num ==''){
                alert("请填写请选择座位号");
                return false;
            }
            if (this.model.code ==''){
                alert("请选择服务员编号");
                return false;
            }
            $.post('/home/index/post.html',{data:JSON.stringify(this.model)},function (res) {
                console.log(res);
                if (res == 1){
                    oPopup.style.display='block';
                    oMask.style.display='block';
                }else{
                    alert("评价失败,请重新评价!");
                    return false;
                }
            },'json');
        }
    }
}).$mount('#app')
